<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
// use app\index\model\Cart as CartModel;

class Cart extends Controller
{
    /**
     * 加入购物车
     */
    public function addCart()
    {
        $param = input('get.');
        if (empty(session('email'))) {
            $this->error('请先登录!', 'login/login');
        }
        if (empty($param['bookID'])) {
            $this->error('请选择商品！', 'index/index');
        }

        $data = Db::table('cart')->where('email', session('email'))->where('bookID', $param['bookID'])->find();

        $num = input('get.num');
        $num = empty($num) ? 1 : $num;

        //$data = Db::table('cart')->where("email='".session('email')."' and flowerID='".$param['flowerID']."'")->find();
        if (empty($data)) {
            $result = Db::execute("insert into cart(cartID,email,bookID,num) values(null,'" . session('email') . "','" . $param['bookID'] . "','" . $num . "')");
            //dump($result);
        } else {
            $result = Db::execute("update cart set num=num+'" . $num . "' where email='" . session('email') . "' and bookID='" . $param['bookID'] . "'");
            //dump($result);
        }

        $this->redirect(url('cart/cart'));
    }

    /**
     * 根据bookID查找cartID
     */
    public function findCartID(){
        $bookID =  input("get.bookID");
        $cart = Db::table('cart')->where("email", session('email'))->where("bookID", $bookID)->find();
        return $cart['cartID'];
    }

    /**
     * 显示我的购物车
     */
    public function cart()
    {
        if (empty(session('email'))) {
            $this->error('请先登录!', 'login/login');
        }
        $data = Db::table('vcart')->where('email', session('email'))->select();

        /* $data=Db::execute("Select cartID,cart.flowerID,pictures,email,num,fname,price,yourprice
                            from cart,flower
		                    where flower.flowerID=cart.flowerID and email='".session('email')."' ");
        */
        $count = count($data);

        $this->assign('result', $data);
        $this->assign('count', $count);
        return $this->fetch();
    }

    /**
     * 通过input更新购物车中某商品的数量
     */
    public function updateCart()
    {
        $param = input('post.');
        $result = Db::execute("update cart set num=" . $param['num'] . " where cartID= '" . $param['cartID'] . "'" . " and bookID = '" . $param['bookID'] . "'");
        if ($result) return "ok";
        return "error";
    }

    /**
     * 将商品从购物车中删除
     */
    public function deleteCart()
    {
        $param = input('get.');
        $result = Db::execute("delete from cart where cartID=" . $param['cartID']);
        if ($result) return "ok";
        return "error";
    }

    /**
     * 批量删除
     */
    public function deleteMore()
    {
        $cartIDs = input('post.cartIDs/a');
        foreach ($cartIDs as $cartID) {
            Db::execute("delete from cart where cartID=" . $cartID);
        }
        // $this->redirect('cart/cart');
    }

    /**
     * 减少商品的数量
     */
    public function Minus()
    {
        $param = input('post.');
        $result = Db::execute("update cart set num=num-1 where cartID='" . $param['cartID'] . "'" . " and bookID = '" . $param['bookID'] . "'");
        if ($result) return "ok";
        return "error";
    }

    /**
     * 增加商品的数量
     */
    public function Plus()
    {
        $param = input('post.');
        $result = Db::execute("update cart set num=num+1 where cartID='" . $param['cartID'] . "'" . " and bookID = '" . $param['bookID'] . "'");
        if ($result) return "ok";
        return "error";
    }
}

/**
 * 说明：一个用户对应多个cartID，1个cartID对应1个商品，数据库建模冗余了
 * ① 如果一个用户对应多台购物车，可以去掉cartID，改为email+bookID做cart表的主键
 * ② 如果一个用户对应一台购物车，cartID不能作为视图vcart的主键，主键为cartID+bookID
 * 
 * 知道就好，懒得改了。
 */
